/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
/**
 * $Id: dxf2lines.cpp,v 1.2 2009-05-13 07:18:25 amaula Exp $
 * \file
 * \brief Simple code to use DIME library for converting the DXF format
 *        files to lines.
 * \author Jari Saarinen <jari.saarinen@aalto.fi>
 *
 * To Compile: g++ -Wall -ldime dxf2lines.cpp -o dxf2lines
 * To run: ./dxf2lines file_in file_out
 *
 */
#include <assert.h>
#include <string>
#include <dime/Input.h>
#include <dime/Model.h>
#include <dime/State.h>
#include <dime/entities/LWPolyline.h>
#include <dime/entities/Line.h>
#include <stdio.h>
#include <string>

// Output stream.
FILE *out = NULL;


bool dcbf(const class dimeState *const s, class dimeEntity *e, void *f)
{
  //  fprintf(stderr,"Got Entity: %s\n", e->getEntityName() );
  if (e->getEntityName() == std::string("LINE")) {
    dimeLine *l = static_cast<dimeLine *>(e);
    assert(l != NULL);

    const dimeVec3f &from = l->getCoords(0);
    const dimeVec3f &to = l->getCoords(1);

    fprintf(out,"%f %f %f %f\n", from.x/1000.0, from.y/1000.0, to.x/1000.0, to.y/1000.0);

} else if (e->getEntityName() == std::string("LWPOLYLINE")) {
    dimeLWPolyline *pl = static_cast<dimeLWPolyline *>(e);
    assert(pl != NULL);
		
		const dxfdouble *X = pl->getXCoords () ;
		const dxfdouble *Y = pl->getYCoords () ;
		
		for(int i=0;i<pl->getNumVertices()-1;i++){
				fprintf(out,"%f %f %f %f\n", X[i]/1000.0, Y[i]/1000.0, X[i+1]/1000.0, Y[i+1]/1000.0);		
		}

} else {
    fprintf(stderr,"Don't know what to do with entity '%s' - Ignore.\n ", e->getEntityName());

}
  return true;
}

int 
main(int argc, char **argv)
{
  char *infile = NULL, *outfile = NULL;

  // param handle - nice :)
  if (argc > 1) { 
    infile = argv[1];
}
  if (argc > 2) {
    outfile = argv[2];
}


  // Inputstream
  dimeInput in;

  
  // Open file
  if (infile == NULL) {
    if (!in.setFileHandle(stdin)) {
      fprintf(stderr,"Unexpected error opening file from stdin\n");
      exit(-1);
}
}
  else {
    if (!in.setFile(infile)) {
      fprintf(stderr,"Error opening file for reading: %s\n", infile);
      exit(-1);
}
}
  
  // Open mode.
  dimeModel model;

  if (!model.read(&in)) {
    fprintf(stderr,"DXF read error in line: %d\n", in.getFilePosition());
    exit(-1);
}


  // Write out lines.
  if (outfile == NULL) {
    out = stdout;
} else {
    out = fopen(outfile, "wb");
    if (out == NULL) {
      perror("Failed to open output file");
      exit(-1);
}
}

  // Get and write lines (uses callback)
  model.traverseEntities(dcbf, 0, true, true, true);

  // Final
  if (out != stdout) fclose(out);
  exit(0);
}
